package com.hyw.supply.service.impl;

import com.hyw.supply.model.entity.SupplyAccount;
import com.hyw.supply.model.vo.LoginUserVO;
import com.hyw.supply.service.SupplyAccountService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zzp
 * @date 2024/3/14 13:10
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final SupplyAccountService supplyAccountService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录信息：{}", username);

        //查询登录信息
        SupplyAccount supplyAccount = supplyAccountService.lambdaQuery()
                .eq(SupplyAccount::getAccount, username)
                .one();
        if (supplyAccount == null) {
            throw new UsernameNotFoundException("登录账号不存在,请检查！");
        }
        List<GrantedAuthority> authorities = new ArrayList<>();
        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(supplyAccount, loginUserVO);
        loginUserVO.setAuthorities(authorities);
        return loginUserVO;
    }
}
